package com.jingdianjichi.subject.domain.service.impl;

import com.jingdianjichi.subject.common.enums.CategoryTypeEnum;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService labelService;

    @Resource
    private SubjectMappingService mappingService;

    @Resource
    private SubjectCategoryService categoryService;

    @Override
    public boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}",subjectLabelBO);
        }

        //这个就是转换的
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        int count = labelService.insert(subjectLabel);

        return count>0;
    }


    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}",subjectLabelBO);
        }

        //这个就是转换的
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);

        int count = labelService.update(subjectLabel);

        return count>0;
    }


    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}",subjectLabelBO);
        }

        //这个就是转换的
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);

        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        int count = labelService.update(subjectLabel);

        return count>0;
    }


    /**
     * 我们现在的做法是,在mapping标中查询categoryId,在查询出来mappping标中个所有的labelId,再去反查询的
     *  /这时其实label表中的,categoryId是没有用的,但是我们先留着
     */
    @Override
    @Transactional
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        /**
         * 如果当前分类是一级分离我们查询所有标签
         */
        SubjectCategory subjectCategory = categoryService.queryById(subjectLabelBO.getCategoryId());

        if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel=new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabel.getCategoryId());

            //如果是一级分类我就把一级分类下所有的标签返回即可,不走下面的逻辑了
            List<SubjectLabel> labelList=labelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> boList=SubjectLabelConverter.INSTANCE.convertLabelToBoList(labelList);

            return boList;
        }

        /**
         * 这里是根据mapping去反查标签的,我们不用这样去做了,因为在新增的时候,我们需要标签来的
         */
        //获得分类id
        Long categoryId = subjectLabelBO.getCategoryId();

        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectMapping> mappingList=mappingService.queryLabelId(subjectMapping);

        //空的直接返回
        if (CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }

        //收集labelId,使用流来收集
        List<Long> LabelList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        //再去差所有的id,你也可以在这里foreach
        List<SubjectLabel> labelList=labelService.batchQueryById(LabelList);

        List<SubjectLabelBO> boList=new ArrayList<>();

        //这里使用mapstruct也是一样的
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();

            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            //bo.setCategoryId(label.getCategoryId());  //只不过我们的现在版本中label表中是没有category字段的,我们不用
            bo.setSortNum(label.getSortNum());
            bo.setCategoryId(categoryId);

            boList.add(bo);
        });

        return boList;
    }

}
